﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CATEGORIES;
using ITEM;
using PAYMENT;

namespace EZXPNS
{
    class EzXpns
    {
        private List<Item> _incomeList;
        private List<Item> _expenseList;

        private decimal _totalIncome;
        private decimal _totalExpense;

        private List<Item> _incomeResult;
        private List<Item> _expenseResult;

        public Stack<Item> _undoDelete;
        public Stack<string> _commands;
        
        public EzXpns()
        {
            _incomeList=new List<Item>();
            _expenseList=new List<Item>();

            _totalExpense = 0m;
            _totalIncome = 0m;

            _incomeResult = new List<Item>();
            _expenseResult = new List<Item>();

            _undoDelete = new Stack<Item>();
            _commands = new Stack<string>(); 

        }

        public EzXpns(List<Item> incomeList,List<Item> expenseList)
        {
            _incomeList = new List<Item>();
            _expenseList = new List<Item>();

            _totalExpense = 0m;
            _totalIncome = 0m;
            
            _incomeResult = new List<Item>();
            _expenseResult = new List<Item>();
            
            _incomeList=incomeList;
            _expenseList=expenseList;

            _undoDelete = new Stack<Item>();
            _commands = new Stack<string>();
        }
        
        public int IncomeSize()
        {
            return _incomeList.Count;
        }

        public int ExpenseSize()
        {
            return _expenseList.Count;
        }

        public void AddIncome(Item incomeItem)
        {
            _commands.Push("ADDINCOME");
            _incomeList.Add(incomeItem);
            _totalIncome += incomeItem.Amount;           
        }

        public void AddExpense(Item expenseItem)
        {
            _commands.Push("ADDEXPENSE");
            _expenseList.Add(expenseItem);
            _totalExpense += expenseItem.Amount;           
        }

        public void DeleteIncome(int index)
        {
            _commands.Push("DELINCOME");
            _undoDelete.Push(_incomeList[index]);
            _totalIncome -= _incomeList[index].Amount;
            _incomeList.RemoveAt(index);
           
           
        }

        public void DeleteExpense(int index)
        {
            _commands.Push("DELEXPENSE");
            _undoDelete.Push(_expenseList[index]);
            _totalIncome -= _expenseList[index].Amount;
            _incomeList.RemoveAt(index);        
        }

        public List<Item> IncomeList
        {
            get { return _incomeList; }
            set { _incomeList = value; }
        }

        public List<Item> ExpenseList
        {
            get { return _expenseList; }
            set { _expenseList = value; }
        }

        public decimal TotalIncome
        {
            get { return _totalIncome; }
        }

        public decimal TotalExpense
        {
            get { return _totalExpense; }
        }

        public void SearchIncome(string infor)
        {
            for (int i = 0; i < _incomeList.Count; i++)
            {
                if (_incomeList[i].Name.Contains(infor) || _incomeList[i].Category.Contains(infor) || _incomeList[i].Date.Contains(infor) || _incomeList[i].Amount.ToString().Contains(infor)||_incomeList[i].Payment.Contains(infor))
                    _incomeResult.Add(_incomeList[i]);
            }
        }

        public void SearchExpense(string infor)
        {
            for (int i = 0; i < _expenseList.Count; i++)
            {
                if (_expenseList[i].Name.Contains(infor) || _expenseList[i].Category.Contains(infor) || _expenseList[i].Date.Contains(infor) || _expenseList[i].Amount.ToString().Contains(infor)||_expenseList[i].Payment.Contains(infor))
                    _expenseResult.Add(_expenseList[i]);
            }
        }

        public void ClearSearchResult()
        {
            _incomeResult.Clear();
            _expenseResult.Clear();
        }

    }
}
